<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight 1.6.3
by Lorenzo Bettini, bettini@gnu.org
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>code/Ch2/mail/MailSystem.java</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<pre>
<tt>
01: <b><font color=#000080>import</font></b> java<font color=#990000>.</font>util<font color=#990000>.</font>ArrayList<font color=#990000>;</font>
02: 
03: <i><font color=#9A1900>/**</font></i>
04: <i><font color=#9A1900>   A system of voice mail boxes.</font></i>
05: <i><font color=#9A1900>*/</font></i>
06: <b><font color=#0000FF>public</font></b> <b><font color=#0000FF>class</font></b> MailSystem
07: <font color=#FF0000>{</font>
08:    <i><font color=#9A1900>/**</font></i>
09: <i><font color=#9A1900>      Constructs a mail system with a given number of mailboxes</font></i>
10: <i><font color=#9A1900>      @param mailboxCount the number of mailboxes</font></i>
11: <i><font color=#9A1900>   */</font></i>
12:    <b><font color=#0000FF>public</font></b> <b><font color=#000000>MailSystem</font></b><font color=#990000>(</font><font color=#009900>int</font> mailboxCount<font color=#990000>)</font>
13:    <font color=#FF0000>{</font>
14:       mailboxes <font color=#990000>=</font> <b><font color=#0000FF>new</font></b> <b><font color=#000000>ArrayList</font></b><font color=#990000>(</font><font color=#990000>)</font><font color=#990000>;</font>
15: 
16:       <i><font color=#9A1900>// Initialize mail boxes.</font></i>
17: 
18:       <b><font color=#0000FF>for</font></b> <font color=#990000>(</font><font color=#009900>int</font> i <font color=#990000>=</font> <font color=#993399>0</font><font color=#990000>;</font> i <font color=#990000>&lt;</font> mailboxCount<font color=#990000>;</font> i<font color=#990000>+</font><font color=#990000>+</font><font color=#990000>)</font>
19:       <font color=#FF0000>{</font>
20:          String passcode <font color=#990000>=</font> <font color=#FF0000>""</font> <font color=#990000>+</font> <font color=#990000>(</font>i <font color=#990000>+</font> <font color=#993399>1</font><font color=#990000>)</font><font color=#990000>;</font>
21:          String greeting <font color=#990000>=</font> <font color=#FF0000>"You have reached mailbox "</font> <font color=#990000>+</font> <font color=#990000>(</font>i <font color=#990000>+</font> <font color=#993399>1</font><font color=#990000>)</font>
22:             <font color=#990000>+</font> <font color=#FF0000>". \nPlease leave a message now."</font><font color=#990000>;</font>
23:          mailboxes<font color=#990000>.</font><b><font color=#000000>add</font></b><font color=#990000>(</font><b><font color=#0000FF>new</font></b> <b><font color=#000000>Mailbox</font></b><font color=#990000>(</font>passcode<font color=#990000>,</font> greeting<font color=#990000>)</font><font color=#990000>)</font><font color=#990000>;</font>
24:       <font color=#FF0000>}</font>
25:    <font color=#FF0000>}</font>
26: 
27:    <i><font color=#9A1900>/**</font></i>
28: <i><font color=#9A1900>      Locate a mailbox.</font></i>
29: <i><font color=#9A1900>      @param ext the extension number</font></i>
30: <i><font color=#9A1900>      @return the mailbox or null if not found</font></i>
31: <i><font color=#9A1900>   */</font></i>
32:    <b><font color=#0000FF>public</font></b> Mailbox <b><font color=#000000>findMailbox</font></b><font color=#990000>(</font>String ext<font color=#990000>)</font>
33:    <font color=#FF0000>{</font>
34:       <font color=#009900>int</font> i <font color=#990000>=</font> Integer<font color=#990000>.</font><b><font color=#000000>parseInt</font></b><font color=#990000>(</font>ext<font color=#990000>)</font><font color=#990000>;</font>
35:       <b><font color=#0000FF>if</font></b> <font color=#990000>(</font><font color=#993399>1</font> <font color=#990000>&lt;</font><font color=#990000>=</font> i <font color=#990000>&amp;</font><font color=#990000>&amp;</font> i <font color=#990000>&lt;</font><font color=#990000>=</font> mailboxes<font color=#990000>.</font><b><font color=#000000>size</font></b><font color=#990000>(</font><font color=#990000>)</font><font color=#990000>)</font>
36:          <b><font color=#0000FF>return</font></b> <font color=#990000>(</font>Mailbox<font color=#990000>)</font> mailboxes<font color=#990000>.</font><b><font color=#000000>get</font></b><font color=#990000>(</font>i <font color=#990000>-</font> <font color=#993399>1</font><font color=#990000>)</font><font color=#990000>;</font>
37:       <b><font color=#0000FF>else</font></b> <b><font color=#0000FF>return</font></b> <b><font color=#0000FF>null</font></b><font color=#990000>;</font>
38:    <font color=#FF0000>}</font>
39: 
40:    <b><font color=#0000FF>private</font></b> ArrayList mailboxes<font color=#990000>;</font>
41: <font color=#FF0000>}</font>
</tt>
</pre>
</body>
</html>
